package mebp.genetic.evolution;

import org.apache.log4j.Logger;

import mebp.genetic.Individuum;
import mebp.genetic.Population;
import mebp.localsearch.ArealMinimizeSearch;

public class Elitist implements EvolutionStrategy {
	int howmany=-1;
	Logger log = Logger.getLogger(ArealMinimizeSearch.class);

	public Elitist(int howMany) {
		this.howmany=howMany;
	}
	
	float percentage=-1;
	public Elitist(float percentage) {
		this.percentage=percentage;
	}
	
	@Override
	public Population newGeneration(Population population) {
		Population ret=new Population();
		int n=howmany;
		if (percentage>0) {
			n=(int) Math.floor(percentage*population.getSize());
		}
		Individuum duplicate=null;
		for (Individuum i:population) { // copy, but remove duplicates
			if (duplicate!=null) {
				if (!duplicate.hasIdenticalGenome(i)) {
					ret.add(i);
					if (--n<0) break;
				} else {
					// log.trace("found dupl");
				}
			} else {
				ret.add(i);
				if (--n<0) break;
			}
			duplicate=i;
		}
		return ret;
	}

}
